param( $ManifestDiff
     , [string]	$SourceDir=$(Throw "Parameter SourceDir is required")
     , [string]	$DestinationDir=$(Throw "Parameter DestinationDir is required")
     , [string] $RollbackDir="'./Rollback'"
     , [switch]	$Whatif
     , [switch]	$Force
     )

begin
{
	
	function CreateSyncScript($ManifestDiff)
	{
		Get-Content (Join-Path $PsSyncDir 'CopyFunctionsTemplate.PS1')
		'function SyncDirectories( [string]$PsSyncSourceDir, [string]$PsSyncDestinationDir, $WhatIf=($false), $Force)'
		'{'
		'  $PsSyncSourceDir = (Get-Item $PsSyncSourceDir).FullName'
		'  $PsSyncDestinationDir = (Get-Item $PsSyncDestinationDir).FullName'

		if ($ManifestDiff.Added.Count -gt 0)
		{
			"  #----------------------------------------------------------"
			"  # Files Added"
			""		
			foreach ($File in $ManifestDiff.Added)
			{
				"  PsSyncAddFile '$($File.RelativePath)'"
			}
			""
		}

		if ($ManifestDiff.Changed.Count -gt 0)
		{
			"  #----------------------------------------------------------"
			"  # Files Changed"
			""		
			foreach ($File in $ManifestDiff.Changed)
			{
				"  PsSyncUpdateFile '$($File.RelativePath)'"
			}
			""
		}
				
		if ($ManifestDiff.Deleted.Count -gt 0)
		{
			"  #----------------------------------------------------------"
			"  # Files Deleted"
			""		
			foreach ($File in $ManifestDiff.Deleted)
			{
				"  PsSyncDeleteFile '$($File.RelativePath)'"
			}
			""
		}
				
		if ($ManifestDiff.Renamed.Count -gt 0)
		{
			"  #----------------------------------------------------------"
			"  # Files Renamed"
			""		
			foreach ($RemamedItem in $ManifestDiff.Renamed)
			{
				"  PsSyncRenameFile '$($RemamedItem.Old.RelativePath)' '$($RemamedItem.New.RelativePath)'"
			}
			""
		}
		"}"
		
        '$RollbackDir = ''' + $RollbackDir + "'"
        
        '$RollbackDir = (InitializeRollback).FullName'
        '$RollbackScript = Join-Path $RollbackDir "Execute-Rollback.Ps1"'
        '$RollbackFiles = Join-Path $RollbackDir "Files"'
        
        'Write-Output "RollbackScript: $RollbackScript"'
        "SyncDirectories '$SourcePath' '$DestinationPath'"
        
	}

	$SourcePath = (Get-Item $SourceDir).FullName
	$DestinationPath = (Get-Item $DestinationDir).FullName

	if ($ManifestDiff -ne $null)
	{
		CreateSyncScript $ManifestDiff
	}
}

process
{
	if ($_ -ne $null)
	{
		CreateSyncScript $_
	}
}
